package org.csu.mypetstore.controller;


import org.csu.mypetstore.common.CommonResponse;
import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CatalogController {
    @Autowired
    public CatalogService catalogService;

    @GetMapping("/categories")
    public CommonResponse<List<Category>> getCategories() {
        return catalogService.getCategoryList();
    }

    @GetMapping("/categories/{id}")
    public CommonResponse<Category> getCategoryById(@PathVariable("id") String categoryId) {
        return catalogService.getCategoryById(categoryId);
    }

    @GetMapping("/products")
    public CommonResponse<List<Product>> getProducts() {
        return catalogService.getProductList();
    }

    @GetMapping("/products/{id}")
    public CommonResponse<Product> getProductById(@PathVariable("id") String productId) {
        return catalogService.getProductById(productId);
    }

    @GetMapping("/categories/{id}/products")
    public CommonResponse<List<Product>> getProductListByCategoryId(@PathVariable("id") String categoryId) {
        return catalogService.getProductListByCategoryId(categoryId);
    }

    @GetMapping("/items")
    public CommonResponse<List<Item>> getItems() {
        return catalogService.getItemList();
    }

    @GetMapping("/items/{id}")
    public CommonResponse<Item> getItemById(@PathVariable("id") String itemId) {
        return catalogService.getItemById(itemId);
    }

    @GetMapping("/products/{id}/items")
    public CommonResponse<List<Item>> getItemListByProductId(@PathVariable("id") String productId) {
        return catalogService.getItemListByProductId(productId);
    }

    @GetMapping("/search/category")
    public CommonResponse<List<Category>> searchCategory(@RequestParam("keyword") String keyword) {
        return catalogService.searchCategory(keyword);
    }

    @GetMapping("/search/product")
    public CommonResponse<List<Product>> searchProduct(@RequestParam("keyword") String keyword) {
        return catalogService.searchProduct(keyword);
    }

    @GetMapping("/search/item")
    public CommonResponse<List<Item>> searchItem(@RequestParam("keyword") String keyword) {
        return catalogService.searchItem(keyword);
    }
}
